#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int T, n, m, K;
bool cx[N][N];
LL seed[N], que[N], a[N];
int getk() {
    int res = -1;
    while ((1<<(++res)) < n);
    return res;
}
int main() {
#ifndef Local
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
#endif
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", seed + i);
    for (int i = 1; i <= m; i++)
        scanf("%d", que + i);
    K = getk();
    getchar();
    for (int i = 1; i <= K; i++) {
        char ch;
        for (int j = 1; j <= (1 << (K - i)); j++) {
            ch = getchar();
            cx[i][j] = ch - '0';
        }
        getchar();
    }
    int X[4];
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d%d", X, X + 1, X + 2, X + 3);
        for (int i = 1; i <= n; i++)a[i] ^= X[i % 4];
        printf("%d\n", rand());
    }
    return 0;
}
